package cn.iocoder.yudao.module.design.service.relatived;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.design.controller.admin.relatived.vo.*;
import cn.iocoder.yudao.module.design.convert.relatived.RelativedConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.relatived.RelativedDO;
import cn.iocoder.yudao.module.design.dal.mysql.relatived.RelativedMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.RELATIVED_NOT_EXISTS;

/**
 * 关联人 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RelativedServiceImpl implements RelativedService {

    @Resource
    private RelativedMapper relativedMapper;

    @Resource
    private AdminUserService adminUserService;
    @Override
    public Long createRelatived(RelativedCreateReqVO createReqVO) {
        // 插入
        RelativedDO relatived = RelativedConvert.INSTANCE.convert(createReqVO);
        relativedMapper.insert(relatived);
        // 返回
        return relatived.getId();
    }

    @Override
    public void updateRelatived(RelativedUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRelativedExists(updateReqVO.getId());
        // 更新
        RelativedDO updateObj = RelativedConvert.INSTANCE.convert(updateReqVO);
        relativedMapper.updateById(updateObj);
    }

    @Override
    public void deleteRelatived(Long id) {
        // 校验存在
        this.validateRelativedExists(id);
        // 删除
        relativedMapper.deleteById(id);
    }

    private void validateRelativedExists(Long id) {
        if (relativedMapper.selectById(id) == null) {
            throw exception(RELATIVED_NOT_EXISTS);
        }
    }

    @Override
    public RelativedDO getRelatived(Long id) {
        return relativedMapper.selectById(id);
    }

    @Override
    public List<RelativedDO> getRelativedList(Collection<Long> ids) {
        return relativedMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RelativedDO> getRelativedPage(RelativedPageReqVO pageReqVO) {
        return relativedMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RelativedDO> getRelativedList(RelativedExportReqVO exportReqVO) {
        return relativedMapper.selectList(exportReqVO);
    }

    @Override
    public List<Map> getFriendList() {
        return relativedMapper.getFriendList(SecurityFrameworkUtils.getLoginUserId() ,null);
    }

    @Override
    public List<Map<String, Object>> getSimFriendList() {
        return relativedMapper.getSimFriendList(SecurityFrameworkUtils.getLoginUserId() ,null);
    }

    /**
     * 查询设计师名下关联人
     *
     * @param vo
     * @return
     */
    @Override
    public List<Map> getRelativedRelList(RelativedBaseVO vo) {
        final Long userId = SecurityFrameworkUtils.getLoginUserId();
        final AdminUserDO user = adminUserService.getUser(userId);
        List<Map>  resultList = new ArrayList<>();
        // 默认 将自己添加至处理人集合中
        Map map = new HashMap();
        map.put("relUserId",userId);
        map.put("mobile",user.getMobile());
        map.put("nickName",user.getNickname());
        map.put("roleName","");
        resultList.add(map);
        final List<Map> relativedRelList = relativedMapper.getRelativedRelList(userId.toString(), vo.getRelUserCd());
        if(!relativedRelList.isEmpty()){
            resultList.addAll(relativedRelList);
        }
        return resultList;
    }



    /**
     * 查询设计师名下关联供应商
     *
     * @param vo
     * @return
     */
    @Override
    public List<RelativedSupplierVO> getRelativedRelSupplierList(RelativedBaseVO vo) {
        final Long userId = SecurityFrameworkUtils.getLoginUserId();

        final List<RelativedSupplierVO> relativedRelList = relativedMapper.getRelativedRelSupplierList(userId.toString(), vo.getRelUserCd());

        return relativedRelList;
    }


    public List<RelativedDO> getRelativedList(Long userId,String statusCd ){
        return relativedMapper.selectListByUserId(userId,statusCd);
    }
}
